package com.puyo;
//Program to build a Puyo-Puyo Game
import javax.swing.*;	
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.StringTokenizer;
import java.applet.*;	
import java.net.*;	
import javax.swing.event.*;
import java.io.*;
public class PuyoPuyoGame extends JFrame
{
	PaintPane gp;
	//LevelDialog d;
	//PauseDialog p;
	public PuyoPuyoGame() 
	{
		super("Puyo-Puyo Game");
		gp=new PaintPane(this);
		Container c=getContentPane();
		c.add(gp);
		setResizable(false);
		setBounds (300,50,330,438);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//d.showDialog();
		gp.setDelays();
	}
	public static void main(String args[]) {
		MenuPane mp=new MenuPane(false);
		mp.setVisible(true);
		//new PuyoPuyoGame();
	}
}
class PaintPane extends JPanel implements ActionListener
{
	int smileys[][]=new int[12][6];			//smileys array holds the information about smileys to display
	int x1,y1,x2,y2,c1,c2;					//Holds the moving smileys information
	String s;								//Formation of chain combo is checked using this string
	Timer t1,t2,t3;							//different timers used for placing delays
	Image images[]=new Image[4];			//holds 4 diff colored smileys
	Random randNumber;						//this object used to generate random numbers
	int rotation;							//used for the rotation of smileys
	boolean reached;						//is smileys reached the ground or not
	int count;								//count of puyos when formed a chain combo
	static int level;						//level of the game
	int score,erasedSmileys;
	MenuPane mp;
	PuyoPuyoGame ppg;	
	int nextPuyo1=0,nextPuyo2=0;
	boolean mouseOver=false;
	public PaintPane(PuyoPuyoGame ppg)
	{
		this.ppg=ppg;
		newGame();
		randNumber=new Random();
		t1=new Timer(1100,this);			
		t1.setInitialDelay(0);				
		t2=new Timer(1000,this);
		t3=new Timer(500,this);
		images[0]=(new ImageIcon("images//puyo_blue.png")).getImage();
		images[1]=(new ImageIcon("images//puyo_green.png")).getImage();
		images[2]=(new ImageIcon("images//puyo_red.png")).getImage();
		images[3]=(new ImageIcon("images//puyo_yellow.png")).getImage();
		startFall();
		startFall();
		setFocusable(true);			
		addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
				setDelays();
				if(e.getKeyCode()==DefineControls.KEY_LEFT && !reached )
				{
					if(y1>0 && y2>0)		//move the smileys one step left 
					{
						if(x1==x2)
						{
							if(y1>y2)
							swap();
							if(smileys[x1][y1-1]==0)
							{
								smileys[x2][y2]=0;
								smileys[x1][--y1]=c1;
								smileys[x2][--y2]=c2;
							}
						}
						else if(smileys[x1][y1-1]==0 && smileys[x2][y2-1]==0)
						{
							smileys[x1][y1]=0;
							smileys[x2][y2]=0;
							smileys[x1][--y1]=c1;
								smileys[x2][--y2]=c2;
						}
					}
					repaint();
				}
				else if(e.getKeyCode()==DefineControls.KEY_RIGHT && !reached)
				{
					if(y1<5 && y2<5)		//move the smileys one step right
					{
						if(x1==x2)
						{
							if(y1<y2)
							swap();
							if(smileys[x1][y1+1]==0)
							{
								smileys[x2][y2]=0;
								smileys[x1][++y1]=c1;
								smileys[x2][++y2]=c2;
							}
						}
						else if(smileys[x1][y1+1]==0 && smileys[x2][y2+1]==0)
						{
								smileys[x1][y1]=0;
								smileys[x2][y2]=0;
								smileys[x1][++y1]=c1;
								smileys[x2][++y2]=c2;
						}
					}
					repaint();
				}
				else
				if(e.getKeyCode()==DefineControls.KEY_UP && !reached)
				{
					if(rotation==1)			//rotate the smileys by 90 degrees clock wise direction
					{
						if(x1>x2)
						swap();
						if(y1>0 && smileys[x1][y1-1]==0)
						{
								smileys[x2][y2]=0;
								smileys[--x2][--y2]=c2;
								rotation=2;
						}
						else
						if(y1<5 && smileys[x1][y1+1]==0)
						{
								smileys[x2][y2]=0;
								smileys[x1][++y1]=c1;
								smileys[--x2][y2]=c2;
								rotation=2;
						}
					}
					else if(rotation==2)
					{
						if(y1>y2)
						swap();
						if(x1>0)
						{
							smileys[x1][y1]=0;
							smileys[--x1][++y1]=c1;
							rotation=3;
						}
						
					}
					else if(rotation==3)
					{
						if(x1<x2)
						swap();
						if(y1<5 && smileys[x1][y1+1]==0)
						{
								smileys[x2][y2]=0;
								smileys[++x2][++y2]=c2;
								rotation=4;
						}
						else
						if(y1>0 && smileys[x1][y1-1]==0)
						{
								smileys[x2][y2]=0;
								smileys[x1][--y1]=c1;
								smileys[++x2][y2]=c2;
								rotation=4;
						}
					}
					else if(rotation==4)
					{
						if(y1<y2)
						swap();
						if(x1<11 && smileys[x1+1][y1-1]==0)
						{
							smileys[x1][y1]=0;
							smileys[++x1][--y1]=c1;
							rotation=1;
						}
					}
					repaint();
				}
				else
				if(e.getKeyCode()==DefineControls.KEY_DOWN)
				{
					if(x1<x2)					//move the smileys one step down
					swap();
					if(x1<11)
					{
						if(smileys[x1+1][y1]==0)
						{
							smileys[x1][y1]=0;
							smileys[++x1][y1]=c1;
						}
						else
						reached=true;
					}
					if(x2<11)
					{
						if(smileys[x2+1][y2]==0)
						{
							smileys[x2][y2]=0;
							smileys[++x2][y2]=c2;
						}
						else
						reached=true;
					}
					repaint();
				}
			}		
		});
		addMouseListener(new MouseAdapter()
		{
			int x=213,y=300,w=100,h=30;
			public void mouseClicked(MouseEvent e)
			{
				if(e.getX()>x && e.getX()<x+w && e.getY()>y && e.getY()<y+h)
				{
					pauseGame();
					showMenu(true);
				}
			}
		});
		addMouseMotionListener(new MouseMotionAdapter()
		{
			int x=213,y=300,w=100,h=30;
			public void mouseMoved(MouseEvent e)
			{
				if(e.getX()>x && e.getX()<x+w && e.getY()>y && e.getY()<y+h)
				mouseOver=true;
				else
				mouseOver=false;
				repaint();
			}
		});
	}
	public void showMenu(boolean b)
	{
		mp=new MenuPane(b);
		mp.setPaintPane(this,ppg);
		mp.setVisible(true);
		ppg.setVisible(false);
	}	
	public void pauseGame()
	{
		t1.stop();
	}
	public void resumeGame()
	{
		t1.start();
	}
	public void newGame()			//start new game by resetting variables
	{
		for(int i=0;i<12;i++)
		for(int j=0;j<6;j++)
		smileys[i][j]=0;
		rotation=1;
		reached=true;
		count=0;
		score=0;
		erasedSmileys=0;
	}
	public static void setLevel(int l)
	{
		level=l;
	}
	public void setDelays()
	{
		t1.setDelay(1100-level*100);
		t1.start();
	}
	public void startFall()			//Start the fall of new Smileys
	{//Checking Top of the game window is occupied by any smileys, if occupied then game is over
		
		if(smileys[0][3]==0 && smileys[1][3]==0)
		{
			c1=smileys[0][3]=nextPuyo1;
			c2=smileys[1][3]=nextPuyo2;
			int r;					
			while((r=randNumber.nextInt(5))==0);//select random color from 4 colors
			nextPuyo1=r;	
			while((r=randNumber.nextInt(5))==0);//select random color from 4 colors
			nextPuyo2=r;
			x1=0;y1=3;x2=1;y2=3;
			rotation=1;
			reached=false;
		}
		else
		{					//game is over
			t1.stop();
			String s=JOptionPane.showInputDialog(null,"Enter Your Name","Congratulations:",JOptionPane.PLAIN_MESSAGE);
			showMenu(false);
			if(s!=null)
			mp.addScore(s,score);
		}
	}
	public void swap()
	{
		int temp=x1;x1=x2;x2=temp;
		temp=y1;y1=y2;y2=temp;
		temp=c1;c1=c2;c2=temp;
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==t1)	//If Event is generated by the t1 object
		{
			boolean reach=true;	
			if(x1<x2)			//Move smileys one step down
			swap();
			if(x1<11)			
			{
				if(smileys[x1+1][y1]==0)
				{
					smileys[x1][y1]=0;
					smileys[++x1][y1]=c1;
					reach=false;
				}
				else
				reached=true;
			}
			if(x2<11)
			{
				if(smileys[x2+1][y2]==0)
				{
					smileys[x2][y2]=0;
					smileys[++x2][y2]=c2;
					reach=false;
				}
				else
				reached=true;
			}	
			if(reach)
			eraseSmileys();			//so smileys reached the ground so erase Smiley which form chain combos
		}							//t2 and t3 are used for make delay b/w erasing puyos and filling vacated places by smileys
		else if(e.getSource()==t2)  
		eraseSmileys();
		else if(e.getSource()==t3)
		{
			for(int i=10;i>=0;i--)			//vacated places formed by erased smileys are filled with the other smileys
			for(int j=0;j<6;j++)
			if(smileys[i][j]>0)				
			{
				int k;
				for(k=i+1;k<=11;k++)
				if(smileys[k][j]>0)			//any smiley exist below the current smiley up to the ground then move on to it
				{
					smileys[k-1][j]=smileys[i][j]; 
					if(i!=k-1)
					smileys[i][j]=0;
					break;
				}
				else if(k==11)		         //if no smiley exist below the current smiley up to the ground then move on to the ground
				{
					smileys[11][j]=smileys[i][j];	
					if(i!=11)
					smileys[i][j]=0;
				}
			}		
			t3.stop();
		}
		repaint();
	}
	public void eraseSmileys()
	{
		boolean erased=false;
		for(int i=0;i<12;i++)
		for(int j=0;j<6;j++)
		if(smileys[i][j]>0)				//for all smiley
		{
			count=1;
			s="";
			arrangeChainCombo(i,j);	
			if(count>=4)				//if chain combo form with 4 or more smileys
			{
				StringTokenizer st=new StringTokenizer(s);
				int rem[][]=new int[st.countTokens()/2][2];
				while(st.hasMoreTokens())
				smileys[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())]=0;
				erasedSmileys+=count;
				if(erasedSmileys>=72)	//Change the level of the game depending on number of removed puyos.
				{								
					level+=1;
					if(level==11)
					{
						t2.stop();
						JOptionPane.showMessageDialog(null,"****************/////// Congratulations ///////**************","Message:",JOptionPane.PLAIN_MESSAGE);
						newGame();
						setDelays();
						startFall();
						
						return;
					}
					setDelays();
					erasedSmileys=0;
				}	
				score+=10*count;		
				erased=true;
			}
		}
		if(erased)
		{
			t1.stop();	  
			t2.start();	 
			t3.start();	
			return;
		}	
		t2.stop();
		if(!t1.isRunning())
		t1.start();			
		startFall();
	}
	public void arrangeChainCombo(int x,int y)
	{
		s=s+" "+x+" "+y;
		if(y<5 && smileys[x][y]==smileys[x][y+1] && !isAlreadyExists(x,y+1))
		{								//check for the same color smiley at the right side of 
			count++;					//current smiley which is not already added to the chain combo
			arrangeChainCombo(x,y+1);	//Then check this smiley can connected to any same color smiley
		}
		if(x<11 && smileys[x][y]==smileys[x+1][y] && !isAlreadyExists(x+1,y))
		{
			count++;
			arrangeChainCombo(x+1,y);
		}
		if(y>0 && smileys[x][y]==smileys[x][y-1] && !isAlreadyExists(x,y-1))
		{
			count++;
			arrangeChainCombo(x,y-1);
		}
		if(x>0 && smileys[x][y]==smileys[x-1][y] && !isAlreadyExists(x-1,y))
		{
			count++;
			arrangeChainCombo(x-1,y);
		}
	}
	public boolean isAlreadyExists(int x,int y) //is the smiley already in the chain combo or not
	{
		StringTokenizer st=new StringTokenizer(s);
		while(st.hasMoreTokens())
		{
			int a=Integer.parseInt(st.nextToken());
			int b=Integer.parseInt(st.nextToken());
			if(x==a && y==b)
			return true;
		}
		return false;
	}
	public void paint(Graphics g)
	{
		g.setColor(Color.gray);
		g.fill3DRect(0,0,getWidth(),getHeight(),false);
		g.setColor(Color.white);
		g.fill3DRect(10,10,193,384,true);	//background fill with black color
		g.fill3DRect(213,10,36,68,true);
		g.drawImage(images[nextPuyo1-1],215,12,32,32,null);
		g.drawImage(images[nextPuyo2-1],215,44,32,32,null);
		for(int i=0;i<12;i++)
		for(int j=0;j<6;j++)
		if(smileys[i][j]>0)			//display all the smileys
		g.drawImage(images[smileys[i][j]-1],j*32+10,i*32+10,32,32,null);
		g.setFont(new Font("Times New Roman",Font.BOLD,20));
		g.setColor(Color.white);
		g.fill3DRect(213,150,100,30,true);
		g.fill3DRect(213,180,100,30,true);
		g.setColor(Color.black);
		g.drawString("Score",238,170);
		g.setFont(new Font("Times New Roman",0,20));	
		g.drawString(""+score,220,200);
		g.setColor(Color.white);
		g.fill3DRect(213,220,100,30,true);
		g.fill3DRect(213,250,100,30,true);
		g.setColor(Color.black);
		g.setFont(new Font("Times New Roman",Font.BOLD,20));
		g.drawString("Speed",235,240);
		g.setFont(new Font("Times New Roman",0,20));	
		g.drawString(""+level,220,270);
		if(mouseOver)
		g.setColor(Color.yellow);
		else
		g.setColor(Color.white);
		g.fill3DRect(213,300,100,30,true);
		g.setColor(Color.black);
		g.setFont(new Font("Times New Roman",Font.BOLD,20));
		g.drawString("Menu",238,320);
	}		
}
class MenuPane extends JFrame implements ActionListener
{
	JButton resume_game;
	JButton new_game;
	JButton options;
	JButton high_score;
	JButton help;
	JButton exit;
	JPanel c;
	JPanel MenuPan;
	JPanel bannerPan;
	CardLayout cl;
	Container cont;
	PuyoPuyoGame ppg;
	PaintPane p;
	boolean paused=false;
	OptionPane op=new OptionPane(this);
	JPanel HighScorePan;
	JPanel HighScoreBanner;
	JPanel scorePan;
	JButton menu;
	JButton menu1;
	JPanel HelpPane;
	JTextArea ta;
	JScrollPane sb;
	
	BufferedReader readhelp;
	
	String highScores[][]=new String[10][2];
	
	JLabel Scores[][]=new JLabel[10][2];
	
	public MenuPane(boolean paused)
	{
		
		this.paused=paused;
		c=new JPanel();
		bannerPan=new JPanel(); 
		MenuPan=new JPanel();
		cont=getContentPane();
		cl=new CardLayout();
		cont.setLayout(cl);
		
		HighScorePan=new JPanel();
		HighScoreBanner=new JPanel();
		scorePan=new JPanel();
		
		MenuPan.setLayout(new BorderLayout());
		GridLayout gd=new GridLayout(paused?9:8,3);
		gd.setVgap(4);
		gd.setHgap(4);
		c.setLayout(gd);
		resume_game=new JButton("Resume Game");
		new_game=new JButton("New Game");
		options=new JButton("Options");
		high_score=new JButton("High Scores");
		help=new JButton("Help");
		exit=new JButton("Exit");
		resume_game.addActionListener(this);
		new_game.addActionListener(this);
		options.addActionListener(this);
		high_score.addActionListener(this);
		help.addActionListener(this);
		exit.addActionListener(this);
		if(paused)
		{
			c.add(new Label(""));
			c.add(resume_game);
			c.add(new Label(""));
		}
		c.add(new Label(""));
		c.add(new_game);
		for(int i=0;i<=1;i++)
		c.add(new Label(""));
		c.add(options);
		for(int i=0;i<=1;i++)
		c.add(new Label(""));
		c.add(high_score);
		for(int i=0;i<=1;i++)
		c.add(new Label(""));
		c.add(help);
		for(int i=0;i<=1;i++)
		c.add(new Label(""));
		c.add(exit);
		for(int i=0;i<=9;i++)
		c.add(new Label(""));
	
		JLabel img=new JLabel("");
		img.setIcon(new ImageIcon("banner.png"));
		bannerPan.add(img);
		MenuPan.add(bannerPan,BorderLayout.NORTH);
		MenuPan.add(c,BorderLayout.CENTER);
		
		
		JLabel img1=new JLabel("");
		img1.setIcon(new ImageIcon("HighScoreBanner.png"));
		HighScoreBanner.add(img1);
		
		scorePan.setLayout(new GridLayout(13,3));
		scorePan.add(new JLabel(""));
		scorePan.add(new JLabel("Name"));
		scorePan.add(new JLabel("Score"));
		
		for(int i=0;i<10;i++)
		for(int j=0;j<2;j++)
		highScores[i][j]="";
		
		try{
				ta=new JTextArea(21,20);
				readhelp=new BufferedReader(new FileReader(new File("Readme.txt")));
				readHighScores();
				readHelpFile();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Error in reading file");
		}
		
		for(int i=0;i<10;i++)
		{
			scorePan.add(new JLabel(""+i));
			scorePan.add((Scores[i][0]=new JLabel()));
			scorePan.add((Scores[i][1]=new JLabel()));
		}
		setScoreLabels();
		
		scorePan.add(new JLabel(""));
		scorePan.add(new JLabel(""));
		
		menu=new JButton("Menu");
		menu.addActionListener(this);
		scorePan.add(menu);
		
		scorePan.add(new JLabel(""));
		scorePan.add(new JLabel(""));
		scorePan.add(new JLabel(""));
		
		HighScorePan.add(HighScoreBanner,BorderLayout.NORTH);
		HighScorePan.add(scorePan,BorderLayout.CENTER);
		
		
		HelpPane=new JPanel();
		
		sb=new JScrollPane(ta);
		
		HelpPane.setLayout(new BorderLayout());
		HelpPane.add(sb,BorderLayout.NORTH);
		
		menu1=new JButton("Menu");
		menu1.addActionListener(this);
		JPanel menuBut=new JPanel();
		menuBut.add(menu1);
		
		HelpPane.add(menuBut,BorderLayout.CENTER);
		
		cont.add(MenuPan,"card1");
		cont.add(HighScorePan,"card2");
		cont.add(HelpPane,"card3");
		
		setBounds(300,50,300,438);
		setVisible(false);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void readHighScores()throws Exception
	{
		int i=0;
		String s;
		BufferedReader br=new BufferedReader(new FileReader(new File("HighScores.txt")));
		while((s=br.readLine())!=null)
		{
			highScores[i][0]=s;
			highScores[i][1]=br.readLine();
			i++;
		}
		br.close();
	}
	public void writeHighScores()throws Exception
	{
		BufferedWriter bw=new BufferedWriter(new FileWriter(new File("HighScores.txt")));
		for(int i=0;i<10;i++)
		{
			bw.write(highScores[i][0]);
			bw.newLine();
			bw.write(highScores[i][1]);
			bw.newLine();
		}
		bw.flush();
		bw.close();
		setScoreLabels();
	}
	public void addScore(String name,int score)
	{
		for(int i=0;i<10;i++)
		{
			String s;
			System.out.println(highScores[i][1]);
			if(highScores[i][1].equals(""))
			s="-1";
			else 
			s=highScores[i][1];
			
			if(Integer.parseInt(s)<score)
			{
				moveScores(i);
				highScores[i][0]=name;
				highScores[i][1]=""+score;
				try{
					writeHighScores();
					printHighScores();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				break;
			}
		}
	}
	public void moveScores(int index)
	{
		for(int i=8;i>=index;i--)
		{
			
			highScores[i+1][0]=highScores[i][0];
			highScores[i+1][1]=highScores[i][1];
			
		}
		
	}
	public void setScoreLabels()
	{
		for(int i=0;i<10;i++)
		for(int j=0;j<2;j++)
		Scores[i][j].setText(highScores[i][j]);
	}
	public void readHelpFile()throws Exception
	{
		String s;
		String helpText="";
		while((s=readhelp.readLine())!=null)
		{
			helpText=helpText+s+"\n";
		}
		readhelp.close();
		ta.setText(helpText);
	}
	public void printHighScores()
	{
		for(int i=0;i<10;i++)
		for(int j=0;j<2;j++)
		System.out.println(highScores[i][j]+" sd ");
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==resume_game)
		{
			setVisible(false);
			ppg.setVisible(true);
			p.resumeGame();
		}	
		if(e.getSource()==new_game)
		{
			setVisible(false);
			ppg=new PuyoPuyoGame();
		}
		else if(e.getSource()==options)
		{
			setVisible(false);
			op.setVisible(true);
		}
		else if(e.getSource()==high_score)
		{
			cl.show(cont,"card2");
		}
		else if(e.getSource()==exit)
		{
			System.exit(0);
		}
		else if(e.getSource()==menu)
		{
			cl.show(cont,"card1");
		}
		else if(e.getSource()==help)
		cl.show(cont,"card3");
		else if(e.getSource()==menu1)
		{
			cl.show(cont,"card1");
		}
	}
	public void setPaintPane(PaintPane p,PuyoPuyoGame ppg)
	{
		this.p=p;
		this.ppg=ppg;
	}
}
class DefineControls
{
	static int KEY_LEFT=KeyEvent.VK_LEFT;
	static int KEY_RIGHT=KeyEvent.VK_RIGHT;
	static int KEY_DOWN=KeyEvent.VK_DOWN;
	static int KEY_UP=KeyEvent.VK_UP;
}
class OptionPane extends JFrame implements ActionListener
{
	JPanel optionPane;
	JPanel controlsPane;
	JPanel optionBanner;
	JButton moveLeft,moveRight,moveDown,rotate,reset,menu;
	JLabel moveLeftl,moveRightl,moveDownl,rotatel,resetl;
	int setControl=0;
	MenuPane mp;
	public OptionPane(MenuPane mp)
	{
		this.mp=mp;
		Container optionPane=getContentPane();
		controlsPane=new JPanel();
		optionBanner=new JPanel();
		
		optionPane.setLayout(new BorderLayout());
		GridLayout gd=new GridLayout(6,2);
		gd.setVgap(10);
		gd.setHgap(10);
		controlsPane.setLayout(gd);
		
		moveLeft=new JButton("Move Left"); 
		moveRight=new JButton("move Right"); 
		moveDown=new JButton("Move Down"); 
		rotate=new JButton("Rotate"); 
		reset=new JButton("Reset Defaults");
		menu=new JButton("Menu");
		
		moveLeft.addActionListener(this);
		moveRight.addActionListener(this);
		moveDown.addActionListener(this);
		rotate.addActionListener(this);
		reset.addActionListener(this);
		menu.addActionListener(this);
		
		moveLeftl=new JLabel(KeyEvent.getKeyText(DefineControls.KEY_LEFT));
		moveRightl=new JLabel(KeyEvent.getKeyText(DefineControls.KEY_RIGHT));
		moveDownl=new JLabel(KeyEvent.getKeyText(DefineControls.KEY_DOWN));
		rotatel=new JLabel(KeyEvent.getKeyText(DefineControls.KEY_UP));
		JLabel ks=new JLabel(" Key Settings:");
		ks.setFont(new Font("Arial",Font.BOLD,20));
		controlsPane.add(ks);
		controlsPane.add(new Label(""));
		controlsPane.add(moveLeft);
		controlsPane.add(moveLeftl);
		controlsPane.add(moveRight);
		controlsPane.add(moveRightl);
		controlsPane.add(moveDown);
		controlsPane.add(moveDownl);
		controlsPane.add(rotate);
		controlsPane.add(rotatel);
		controlsPane.add(reset);
		controlsPane.add(menu);
		
		JLabel img1=new JLabel("");
		img1.setIcon(new ImageIcon("optionBanner.png"));
		optionBanner.add(img1);
		optionPane.add(optionBanner,BorderLayout.NORTH);
		optionPane.add(controlsPane,BorderLayout.CENTER);
		
		setBounds(300,50,300,438);
		setVisible(false);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				System.out.println("Print");
				if(setControl==1)
				DefineControls.KEY_LEFT=e.getKeyCode();
				if(setControl==2)
				DefineControls.KEY_RIGHT=e.getKeyCode();
				if(setControl==3)
				DefineControls.KEY_DOWN=e.getKeyCode();
				if(setControl==4)
				DefineControls.KEY_UP=e.getKeyCode();
				setLabels();
				setControl=0;
			}
		});
		setFocusable(true);
	}
	public void setLabels()
	{
		moveLeftl.setText(KeyEvent.getKeyText(DefineControls.KEY_LEFT));
		moveRightl.setText(KeyEvent.getKeyText(DefineControls.KEY_RIGHT));
		moveDownl.setText(KeyEvent.getKeyText(DefineControls.KEY_DOWN));
		rotatel.setText(KeyEvent.getKeyText(DefineControls.KEY_UP));
	}
	public void resetDefaults()
	{
		DefineControls.KEY_LEFT=KeyEvent.VK_LEFT;
		DefineControls.KEY_RIGHT=KeyEvent.VK_RIGHT;
		DefineControls.KEY_DOWN=KeyEvent.VK_DOWN;
		DefineControls.KEY_UP=KeyEvent.VK_UP;
		setLabels();
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==menu)
		{
			mp.setVisible(true);
			setVisible(false);
		}
		else if(e.getSource()==moveLeft)
		{
			setLabels();
			setControl=1;
			moveLeftl.setText("Press a key");
		}
		else if(e.getSource()==moveRight)
		{
			setLabels();
			setControl=2;
			moveRightl.setText("Press a key");
		}
		else if(e.getSource()==moveDown)
		{
			setLabels();
			setControl=3;
			moveDownl.setText("Press a key");
		}
		else if(e.getSource()==rotate)
		{
			setLabels();
			setControl=4;
			rotatel.setText("Press a key");
		}
		else if(e.getSource()==reset)
		{
			resetDefaults();
		}
		menu.nextFocus();
	}
}